package com.jxc.action.jxsgl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.po.DealerGrantRecord;
import com.jxc.model.po.Jhtj;
import com.jxc.model.po.Xstj;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VDealerGrantRecord;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VUser;
import com.jxc.service.DealerServiceI;
import com.jxc.service.DealerTotalServiceI;
import com.jxc.service.UserServiceI;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/jxsgl")
@Action(value = "dealerAction", results = {
		@Result(name = "dealer", location = "/dealer/dealer.jsp"),
		@Result(name = "myDealer", location = "/dealer/myDealer.jsp")})
//		@Result(name = "dealerAdd", location = "/dealer/dealerAdd.jsp") })
public class DealerAction extends BaseAction implements ModelDriven<VDealerGrantRecord>{
	@Autowired
	private DealerServiceI dealerService;
	@Autowired
	private DealerTotalServiceI dealerTotalService;
	@Autowired
	private UserServiceI userService;
	
	private List<Jhtj> dealerlist = new ArrayList<Jhtj>();
	private List<Xstj> xstjlist = new ArrayList<Xstj>();
	private List<VDealerGrantRecord> modellist = new ArrayList<VDealerGrantRecord>();
	
	private VUser vUser = new VUser();
	private VDealerGrantRecord model = new VDealerGrantRecord();
	private String ids ;
	private String brandname;
	private String realName;
	

	public String getBrandname() {
		return brandname;
	}

	public void setBrandname(String brandname) {
		this.brandname = brandname;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public VUser getvUser() {
		return vUser;
	}

	public void setvUser(VUser vUser) {
		this.vUser = vUser;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<VDealerGrantRecord> getModellist() {
		return modellist;
	}

	public void setModellist(List<VDealerGrantRecord> modellist) {
		this.modellist = modellist;
	}

	public String dealer(){
		getSubAuths("JXSGL_JXSCX");	
		return "dealer";
	}
	
	public String myDealer(){
		getSubAuths("JXSGL_MYJXS");	
		return "myDealer";
	}
	
	
	public void datagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
//		vUser = userService.get(sessionInfo.getUserId());
		model.setDealerid(sessionInfo.getUserId());
		System.out.println(realName);
		if(brandname!=null&&!brandname.trim().equals("")){
			vUser.setBrandname(brandname);
		}
		if(realName!=null&&!realName.trim().equals("")){
			vUser.setRealName(realName);
		}
		vUser.setSort(model.getSort());
		vUser.setOrder(model.getOrder());
		vUser.setPage(model.getPage());
		vUser.setRows(model.getRows());
		vUser.setId(sessionInfo.getUserId());//
		//if(vUser.getReid()==null){
			writeJson(userService.dealer(vUser));
		//}
	}
	
	public void myDealerDatagrid() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		model.setDealerid(sessionInfo.getUserId());
		System.out.println(realName);
		if(brandname!=null&&!brandname.trim().equals("")){
			vUser.setBrandname(brandname);
		}
		if(realName!=null&&!realName.trim().equals("")){
			vUser.setRealName(realName);
		}
		vUser.setSort(model.getSort());
		vUser.setOrder(model.getOrder());
		if(vUser.getSort().equals("id")){
			vUser.setSort("isGranted");
			vUser.setOrder("desc");
		}
		vUser.setPage(model.getPage());
		vUser.setRows(model.getRows());
		vUser.setId(sessionInfo.getUserId());
		writeJson(userService.myDealer(vUser));
	}
	
	/**
	 * 查询当前代理商所有已经关联的经销商，给前台一个json
	 */
	public void noAuth_MyDealerJson() {
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
		VUser vuser = userService.get((userService.get(sessionInfo.getUserId())).getUpid());
		List<VUser> dealers = new ArrayList<VUser>();
		dealers = userService.findAllMyDealer(vuser);
		this.writeJson(dealers);
	}
	
	
 
	//jfreechart柱状图
	public String dealerTotal() {
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			dealerlist = userService.findJhsum(sessionInfo.getUserId());
			
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("image/jpeg");
			JFreeChart chart = dealerTotalService.getChart(dealerlist);
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart, 300, 500);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String dealerSaleTotal(){
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			xstjlist = userService.findSalesum(sessionInfo.getUserId());
			
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("image/jpeg");
			
			JFreeChart chart = dealerTotalService.getXsChart(xstjlist);
			ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart, 300, 500);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void add(){
		SessionInfo sessionInfo = (SessionInfo) ServletActionContext
		.getRequest().getSession().getAttribute(
				ResourceUtil.getSessionInfoName());
			model.setAgencyid(sessionInfo.getUserId());
//			model.setIds(ids);
			Integer result = dealerService.save(model);
			Json j = new Json();
			if (result == 1) {
				j.setMsg("添加失败，已经授权过！");
				j.setSuccess(false);
			} else {
				j.setMsg("添加成功！");
				j.setSuccess(true);
			}
			super.writeJson(j);
	}
	public void delete(){
		Json j = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext
			.getRequest().getSession().getAttribute(
					ResourceUtil.getSessionInfoName());
				model.setAgencyid(sessionInfo.getUserId());
//				model.setIds(ids);
				Integer result = dealerService.delete(model,j);
				if (result == 1) {
					j.setMsg("删除成功！"+j.getMsg());
					j.setSuccess(true);
				} else {
					j.setMsg("删除失败！"+j.getMsg());
					j.setSuccess(false);
				}
				super.writeJson(j);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public VDealerGrantRecord getModel() {
		return model;
	}
	
}
